package com.slg.android.listenbook.biz.bookcity.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.slg.android.listenbook.R;
import com.slg.android.listenbook.biz.bookcity.model.BookInfo;
import com.slg.android.listenbook.common.DataStore;

import java.util.LinkedList;

/**
 * Created by Darker on 2015/3/25.
 */
public class CategoryListAdapter extends BaseAdapter {

    private Context context;
    private LinkedList<BookInfo> data;
    public CategoryListAdapter(Context context,LinkedList<BookInfo> data){
        this.context = context;
        this.data = data;

    }

    @Override
    public int getCount() {
        int ret = 0;
        if(data != null){
            ret = data.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BookInfo bookInfo = data.get(position);

        if(convertView == null){
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_category_list_activity,parent,false);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if(holder == null){
            holder = new ViewHolder();
            convertView.setTag(holder);
        }

        holder.announcer = (TextView) convertView.findViewById(R.id.item_category_list_activity_announcer);
        holder.hot = (TextView) convertView.findViewById(R.id.item_category_list_activity_hot);
        holder.sections = (TextView) convertView.findViewById(R.id.item_category_list_activity_sections);
        holder.state = (TextView) convertView.findViewById(R.id.item_category_list_activity_state);
        holder.bookName = (TextView) convertView.findViewById(R.id.item_category_list_activity_name);
        holder.img = (ImageView)convertView.findViewById(R.id.item_category_list_activity_img);

        holder.announcer.setText("播音："+bookInfo.getAnnouncer());
        holder.hot.setText(bookInfo.getHot()+"人气");
        holder.sections.setText(bookInfo.getSections()+"集");

        if("1".equals(bookInfo.getState())){
            holder.state.setTextColor(Color.WHITE);
            holder.state.setText("完结");
        }else{
            holder.state.setTextColor(Color.RED);
            holder.state.setText("连载");
        }

        holder.bookName.setText(bookInfo.getName());

        DataStore dataStore = DataStore.getInstance();
        if(dataStore != null) {
            BitmapUtils bitmapUtils = dataStore.getBitmapUtils();
            bitmapUtils.configDefaultLoadFailedImage(R.drawable.icon_audio_default);
            bitmapUtils.configDefaultLoadingImage(R.drawable.icon_audio_default);
            bitmapUtils.display(holder.img, bookInfo.getCover());
        }
        return convertView;
    }

    public static class ViewHolder{
        public TextView sections;
        public TextView hot;
        public TextView announcer;
        public TextView state;
        public TextView bookName;
        public ImageView img;
    }
}
